/*
 * Copyright (c) 2020, 2025, keyila All rights reserved.
 *
 */
package com.clp.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @author clp
 * @version 1.0
 */
public class AppClient {
    public void run(){
        //定义线程池
        NioEventLoopGroup group=new NioEventLoopGroup();
        // 创建一个用户的辅助类BootStrap
        Bootstrap bootstrap=new Bootstrap();
        bootstrap.group(group)
                .remoteAddress(new InetSocketAddress(8080))
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {

                    }
                });

        // 尝试连接服务器
        try {
            ChannelFuture channelFuture = bootstrap.connect().sync();
            channelFuture.channel().writeAndFlush(Unpooled.copiedBuffer("hello netty".getBytes(Charset.forName("UTF-8"))));
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
